package com.gg.cart.mapper;

import com.gg.entity.Cart;
import com.gg.entity.Fruit;
import com.gg.helper.DBHelper;
import com.gg.povos.CartFruitPovo;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*持久层*/
public class CartMapper {
    /*按uid 查询 cart  fruit */
    public List<CartFruitPovo> selectCartFruitPovosByUid(String uid) {
        List<CartFruitPovo>  povos =  new ArrayList<>();
        Connection con = DBHelper.getConnection();
        String sql = "SELECT  fruit.f_id,\n" +
                "\tfruit.f_name,\n" +
                "\tfruit.f_norm,\n" +
                "\tfruit.f_price,\n" +
                "\tcart.ccount,\n" +
                "\tcart.cid\n" +
                "\tFROM \n" +
                "\tfruit,cart\t\n" +
                "\tWHERE \n" +
                "\tfruit.f_id = cart.fid\n" +
                "\tAND\n" +
                "\tcart.uid = ?";
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,uid);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int  f_id = rs.getInt("f_id");
                String  f_name = rs.getString("f_name");
                String  f_norm = rs.getString("f_norm");
                BigDecimal f_price = rs.getBigDecimal("f_price");
                int  ccount = rs.getInt("ccount");
                String  cid = rs.getString("cid");
                CartFruitPovo povo=  new CartFruitPovo(new Cart(),new Fruit());
                povo.getCart().setCid(cid);
                povo.getCart().setCcount(ccount);
                povo.getFruit().setF_id(f_id);
                povo.getFruit().setF_name(f_name);
                povo.getFruit().setF_norm(f_norm);
                povo.getFruit().setF_price(f_price);
                povos.add(povo);
            }

            return  povos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    /*按主键删除购物车条目*/
    public void deleteCartByCid(String cid) {

        Connection con =  DBHelper.getConnection();
        String sql = "delete from  cart  where cid = ?";
        try {
            PreparedStatement  ps = con.prepareStatement(sql);
            ps.setString(1,cid);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int updateCartByCidForCcount(String cid, Integer count) {
        Connection con = DBHelper.getConnection();
        String sql = "update cart set ccount = ccount + ? where cid = ?";
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,count);
            ps.setString(2,cid);
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    // 添加 insertCart 方法
    public void insertCart(Cart cart, Fruit fruit) {
        Connection con = DBHelper.getConnection();
        String sql = "INSERT INTO cart (cid, uid, fid, ccount) VALUES (?, ?, ?, ?)";
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, cart.getCid());
            ps.setString(2, cart.getUid());
            ps.setInt(3, fruit.getF_id());
            ps.setInt(4, cart.getCcount());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
     // 添加或更新购物车方法
public void addOrUpdateCart(Cart cart, Fruit fruit) {
    Connection con = DBHelper.getConnection();
    String checkSql = "SELECT ccount FROM cart WHERE uid = ? AND fid = ?";
    String updateSql = "UPDATE cart SET ccount = ccount + ? WHERE uid = ? AND fid = ?";
    String insertSql = "INSERT INTO cart (cid, uid, fid, ccount) VALUES (?, ?, ?, ?)";

    try {
        PreparedStatement checkPs = con.prepareStatement(checkSql);
        checkPs.setString(1, cart.getUid());
        checkPs.setInt(2, fruit.getF_id());
        ResultSet rs = checkPs.executeQuery();

        if (rs.next()) {
            // 商品已存在，更新数量
            PreparedStatement updatePs = con.prepareStatement(updateSql);
            updatePs.setInt(1, cart.getCcount()); // 增加传入的数量
            updatePs.setString(2, cart.getUid());
            updatePs.setInt(3, fruit.getF_id());
            updatePs.executeUpdate();
        } else {
            // 商品不存在，插入新记录
            PreparedStatement insertPs = con.prepareStatement(insertSql);
            insertPs.setString(1, cart.getCid());
            insertPs.setString(2, cart.getUid());
            insertPs.setInt(3, fruit.getF_id());
            insertPs.setInt(4, cart.getCcount());
            insertPs.executeUpdate();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

    }



